package com.lhy.noed;

import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.Map;

/**
 * @description: 在GzExecute之上添加对一层静态代理
 * @author: 李汇远
 */
public class GzDao{

    // 数据库表名
    private final String tbName;

    // 操作类
    private final GzExecute gzExecute;

    public GzDao(String tbName, GzExecute execute){
        this.tbName = tbName;
        this.gzExecute = execute;
        if (! execute.getDbInfo().getTableNames().contains(tbName)){
            throw new NullPointerException("[GzDao]未找到表名:"+tbName);
        }
    }

    // 添加新记录
    public Integer add(JSONObject data){
        return gzExecute.add(tbName, data);
    }

    // 删除记录
    public Integer remove(String colName, String colData){
        return gzExecute.remove(tbName, colName, colData);
    }

    /**
     * 删除字段为空值的部分
     * @param colName
     * @return
     */
    public Integer removeNull(String colName){
        return gzExecute.removeNull(tbName, colName);
    }

    /**
     *
     * @return
     */
    public List<Map<String, Object>> get(){
        return gzExecute.get(tbName);
    }

    public List<Map<String, Object>> get(String where){
        return gzExecute.get(tbName, where);
    }

    public List<Map<String, Object>> get(JSONObject params){
        return gzExecute.get(tbName, params);
    }

    public List<Map<String, Object>> get(String colName, String colData){
        return gzExecute.get(tbName, colName, colData);
    }

    /**
     * 更新记录
     * @param content
     * @param where
     * @return
     */
    public Integer update(String content, String where){
        return gzExecute.update(tbName, content, where);
    }

    public Integer update(JSONObject params, String where){
        return gzExecute.update(tbName, params, where);
    }

    public Integer update(String colName, String colData, String where){
        return gzExecute.update(tbName, colData, where);
    }

    /**
     * 获取数据库表对应的JsonObject, key-value 中value默认取 “”
     * @return
     */
    public JSONObject getEntity(){
        return gzExecute.getEntity(tbName);
    }

}
